package service

import (
	"context"
	"time"

	"gitee.com/jiebaiyou/formflow/model/questions"
	"gitee.com/jiebaiyou/formflow/pkg/bizcode"
)

// questionService 实现了QuestionService接口, 提供题目相关的业务逻辑
type questionService struct {
	questionRepository questions.QuestionRepository
	contextTimeout     time.Duration
}

// NewQuestionService 创建并返回一个实现了QuestionService接口的实例
func NewQuestionService(questionRepository questions.QuestionRepository, timeout time.Duration) questions.QuestionService {
	return &questionService{
		questionRepository: questionRepository,
		contextTimeout:     timeout,
	}
}

// GetQuestionTemplates 获取题目模板
func (s *questionService) GetQuestionTemplates(c context.Context, types []questions.QuestionType) (questions.QuestionTemplate, error) {
	ctx, cancel := context.WithTimeout(c, s.contextTimeout)
	defer cancel()

	return s.questionRepository.GetQuestionTemplates(ctx, types)
}

// CreateQuestion 创建题目
func (s *questionService) CreateQuestion(c context.Context, template *questions.QuestionTemplate) error {
	ctx, cancel := context.WithTimeout(c, s.contextTimeout)
	defer cancel()

	// 检查请求中是否缺少有效的题目数据
	if template.IsEmpty() {
		return bizcode.MissingQuestionData
	}

	now := time.Now()

	// 处理单选题
	if template.SingleChoiceQuestion != nil {
		// // 验证基础字段
		// if err := validateBaseQuestion(template.SingleChoiceQuestion.BaseQuestion); err != nil {
		// 	return err
		// }
		// // 验证特有字段
		// if err := validateSingleChoiceQuestion(template.SingleChoiceQuestion); err != nil {
		// 	return err
		// }
		// 补充字段
		// template.SingleChoiceQuestion.ID = generateid.GenerateUUID()
		template.SingleChoiceQuestion.Type = questions.SingleChoice
		template.SingleChoiceQuestion.CreatedAt = &now
		template.SingleChoiceQuestion.UpdatedAt = &now
	}

	// 处理多选题
	if template.MultipleChoiceQuestion != nil {
		// 补充字段
		template.MultipleChoiceQuestion.Type = questions.MultipleChoice
		template.MultipleChoiceQuestion.CreatedAt = &now
		template.MultipleChoiceQuestion.UpdatedAt = &now
	}

	if template.TrueFalseQuestion != nil {
		// 补充字段
		template.TrueFalseQuestion.Type = questions.TrueFalse
		template.TrueFalseQuestion.CreatedAt = &now
		template.TrueFalseQuestion.UpdatedAt = &now
	}

	if template.ShortAnswerQuestion != nil {
		// 补充字段
		template.ShortAnswerQuestion.Type = questions.ShortAnswer
		template.ShortAnswerQuestion.CreatedAt = &now
		template.ShortAnswerQuestion.UpdatedAt = &now
	}

	if template.RatingQuestion != nil {
		// 补充字段
		template.RatingQuestion.Type = questions.Rating
		template.RatingQuestion.CreatedAt = &now
		template.RatingQuestion.UpdatedAt = &now
	}

	if template.FillBlankQuestion != nil {
		// 补充字段
		template.FillBlankQuestion.Type = questions.FillBlank
		template.FillBlankQuestion.CreatedAt = &now
		template.FillBlankQuestion.UpdatedAt = &now
	}

	// 以此类推处理其他题型...

	// 调用repository保存题目
	return s.questionRepository.CreateQuestion(ctx, template)
}

// UpdateQuestion 更新题目
func (s *questionService) UpdateQuestion(c context.Context, questionID string) error {
	ctx, cancel := context.WithTimeout(c, s.contextTimeout)
	defer cancel()

	return s.questionRepository.UpdateQuestion(ctx, questionID)
}

// DeleteQuestion 删除题目
func (s *questionService) DeleteQuestion(c context.Context, questionID string) error {
	ctx, cancel := context.WithTimeout(c, s.contextTimeout)
	defer cancel()

	return s.questionRepository.DeleteQuestion(ctx, questionID)
}

// GetQuestionByID 获取题目
func (s *questionService) GetQuestionByID(c context.Context, questionID string) (any, error) {
	ctx, cancel := context.WithTimeout(c, s.contextTimeout)
	defer cancel()

	return s.questionRepository.GetQuestionByID(ctx, questionID)
}

// ListQuestions 获取题目列表
func (s *questionService) ListQuestions(c context.Context) ([]any, error) {
	ctx, cancel := context.WithTimeout(c, s.contextTimeout)
	defer cancel()

	return s.questionRepository.ListQuestions(ctx)
}
